package com.ctsi.question.service;

import com.ctsi.commons.util.UtilValidate;
import com.ctsi.commons.util.id.UUIDGenerator;
import com.ctsi.commons.util.response.Page;
import com.ctsi.framework.mybatis.support.MybatisGenericDao;
import com.ctsi.question.entity.PlanEntity;
import com.ctsi.question.entity.Question;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @auth:wgf
 * @description :
 * @Date: 2019/7/26
 */
@Service

public class PlanService {
    private static final Logger logger= LoggerFactory.getLogger(PlanService.class);
    @Autowired
    private MybatisGenericDao dao;
    /*
     * 查询当前用户所回答过的问题
     * */
    public Page<Question> selectQuLibByLoginId(Map<String,Object> param , int start , int pageSize){
        logger.debug("selectQuLibByLoginId {}",param);
        if(pageSize<0||pageSize>2000)
            pageSize=Page.DEFAULT_PAGE_SIZE;
        if(start!=-1)
            return dao.queryPage(Question.class.getName()+".selectQuLibByLoginId", param, start, pageSize);
        else{
            throw new RuntimeException("参数不正确 !!");
        }
    }

    /**
     * 添加项目计划
     */
    public int insertItemPlan(PlanEntity planEntity){
        if(UtilValidate.isEmpty(planEntity.getId())){
            planEntity.setId(UUIDGenerator.getId());
        }
        logger.debug("insert {}",planEntity);
        return dao.insert(PlanEntity.class.getName() + ".insert", planEntity);
    }

    /**
     * 动态更新
     */
    public int updateItemPlanById(PlanEntity planEntity){
        logger.debug("updateItemPlanById {}",planEntity);
        return dao.update(PlanEntity.class.getName()+".updateItemPlanById", planEntity);
    }

    /*
     * 更新浏览数量
     * */
    public int updateLiuLanNumById(Question question){
        logger.debug("updateLiuLanNumById {}",question);
        return dao.update(Question.class.getName()+".updateLiuLanNumById",question);
    }

    /**
     * 根据id查询计划详情
     *
     * */
    public PlanEntity selectItemPlanById(String id){
        if(UtilValidate.isEmpty(id))
            return null;
        logger.debug("selectItemPlanById {}",id);
        return (PlanEntity)dao.queryForObject(PlanEntity.class.getName()+".selectItemPlanById", id);
    }

    /**
     * 删除Question对象（is_default=1）
     */
    public int deleteItemPlanById(PlanEntity question){
        logger.debug("deleteItemPlanById {}",question);
        return dao.update(PlanEntity.class.getName()+".deleteItemPlanById", question);
    }

    /**
     * 多条件查询结果
     */
    public Page<PlanEntity> selectMulitCondition(Map<String,Object> param , int start , int pageSize){
        logger.debug("selectMulitCondition {}",param);
        if(pageSize<0||pageSize>2000)
            pageSize=Page.DEFAULT_PAGE_SIZE;
        if(start!=-1)
            return dao.queryPage(PlanEntity.class.getName()+".selectMulitCondition", param, start, pageSize);
        else{
            throw new RuntimeException("参数不正确 !!");
        }
    }
    /**
     * 当前登陆用户所提问题列表
     */
    public Page<Question> queryQuestionListByUserId(Map<String,Object> param , int start , int pageSize){
        logger.debug("queryQuestionListByUserId {}",param);
        if(pageSize<0||pageSize>2000)
            pageSize=Page.DEFAULT_PAGE_SIZE;
        if(start!=-1)
            return dao.queryPage(Question.class.getName()+".queryQuestionListByUserId", param, start, pageSize);
        else{
            throw new RuntimeException("参数不正确 !!");
        }
    }
}
